<?

/*======================================
函数库
规则:
1.函数不能嵌套函数。
这样会造成函数互相依赖，破坏独立性

======================================*/



/*===============================
//传递产品的id，然后根据产品id，查出其所属分类的名字。
//为了输出格式的灵活，所以第二个参数$termnumber用来判断返回term0~term5
===============================
//传递产品的id，查出term0至term5个个分类的名字和id*/
function gettermdate($dataid, $termnumber)
{

    global $table_Prefix;
    global $sys_list_table;
    global $sys_term_table;
//设置需要搜索的表格的前缀后缀
    $table_Suffix = $sys_list_table;
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀


//设置需要搜索的表格的排序项和升降项
    $sort_by = "nid";
    $sort_order = "desc";
//设置需要搜索的表格的排序项和升降项


    $result = mysql_query("SELECT * FROM $table where nid = '$dataid'"); //	查出这个id的产品的各个termid

    while ($row = mysql_fetch_array($result)) {

        $term0 = $row['term0'];
        $term1 = $row['term1'];
        $term2 = $row['term2'];
        $term3 = $row['term3'];
        $term4 = $row['term4'];
        $term5 = $row['term5'];

    }

//	根据这些term的id查出各自的名字


    $table_Suffix = $sys_term_table;
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀


//设置需要搜索的表格的排序项和升降项
    $sort_by = "nid";
    $sort_order = "desc";
//设置需要搜索的表格的排序项和升

    $result = mysql_query("SELECT * FROM $table where nid = '$term0'");

    while ($row = mysql_fetch_array($result)) {
        $term0_name = $row['name'];
    }


    $result = mysql_query("SELECT * FROM $table where nid = '$term1'");

    while ($row = mysql_fetch_array($result)) {
        $term1_name = $row['name'];
    }


    $result = mysql_query("SELECT * FROM $table where nid = '$term2'");

    while ($row = mysql_fetch_array($result)) {
        $term2_name = $row['name'];
    }

    $result = mysql_query("SELECT * FROM $table where nid = '$term3'");

    while ($row = mysql_fetch_array($result)) {
        $term3_name = $row['name'];
    }


    $result = mysql_query("SELECT * FROM $table where nid = '$term4'");

    while ($row = mysql_fetch_array($result)) {
        $term4_name = $row['name'];
    }


    $result = mysql_query("SELECT * FROM $table where nid = '$term5'");

    while ($row = mysql_fetch_array($result)) {
        $term5_name = $row['name'];
    }

    mysql_close($con);


    if ($termnumber == 1) {
        return $term1_name;
    } else if ($termnumber == 2) {
        return $term2_name;
    } else if ($termnumber == 3) {
        return $term3_name;
    } elseif ($termnumber == 4) {
        return $term4_name;
    } else // if($termnumber==5)
    {
        return $term5_name;
    }

//	查出t1直到t5的名字，然后输出


}










//后台用检查账号等级。
function check_admin_type()
{


//根据传过来的session账户名，确定账户的类型，从而判断权限

    global $table_Prefix;
//设置需要搜索的表格的前缀后缀
    $table_Suffix = "login_admin";
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀


//设置需要搜索的表格的排序项和升降项
    $sort_by = "nid";
    $sort_order = "desc";
//设置需要搜索的表格的排序项


    $check_name = $_SESSION['username'];

    $result = mysql_query("select * from $table where name = '$check_name' ");

    while ($row = mysql_fetch_array($result)) {
        $admin_nid = $row['nid'];
        $admin_type_now=$row['admin_term'];
        $admin_password = $row['admin_password'];
    }


    if ($admin_type_now == 1) {
        return "admins";
    } elseif ($admin_type_now == 2) {
        return "editor";
    }

}






////截取utf8字符串
//function utf8Substr($str, $from, $len)
//{
//    return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $from . '}' .
//        '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $len . '}).*#s',
//        '$1', $str);
//}
//







//获取数据库中id最大值

function get_max_nid($term_or_detail)
{
    global $table_Prefix;
    global $sys_term_table; //板块的分类表格名
    global $sys_list_table; //板块的详细表格
//设置需要搜索的表格的前缀后缀


    $table_Suffix = "detail";
    if ($term_or_detail == "term") {
        $table_Suffix = "term";
    }

    $table = $table_Prefix . $table_Suffix;

    $result = mysql_query("select * from $table where nid=(select max(nid) from $table) ");

    while ($row = mysql_fetch_array($result)) {
        $max_nid = $row['nid'];
    }

    return $max_nid + 1;
}




//传递单个需要对比的变量和变量数组，
//检测单个变量是否存在于变量数组中。

function compare_term($target, $target_list)
{
    $term_exist = false;

    for ($i = 0; $i < count($target_list); $i++) {
        if ($target == $target_list[$i]) {
            $term_exist = true;
        }
    }

    return $term_exist;
}





//获取分类id，然后给对用id的数据，clickrate加1
function term_clickrate($trans_nid, $term_detail_clickrate)
{

    $term_detail_clickrate = $term_detail_clickrate + 1;


    global $table_Prefix;
    global $sys_list_table;
    global $sys_term_table;
//设置需要搜索的表格的前缀后缀
    $table_Suffix = $sys_term_table;
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀


//开始把点击率插入数据库
    $clickrate_increase = "update $table SET
	clickrate          =    '$term_detail_clickrate'
	where nid='$trans_nid'";

    $query = mysql_query($clickrate_increase) or die ("error:".mysql_error());


}






//获取分类id，然后给对用id的数据，clickrate加1
function detail_clickrate($trans_nid, $detail_clickrate)
{

    $detail_clickrate = $detail_clickrate + 1;

    global $table_Prefix;
    global $sys_list_table;

    $table_Suffix = $sys_list_table;
    $table = $table_Prefix . $table_Suffix;

    $clickrate_increase = "update $table SET clickrate = '$detail_clickrate' where nid='$trans_nid'";
    $query = mysql_query($clickrate_increase) or die ("error:".mysql_error());


}






//用户名片展示系统浏览记录
function usercard_click($userId,$userClick){
    $userClick = $userClick + 1;
    global $table_Prefix;
    $table_Suffix = "user";
    $table = $table_Prefix . $table_Suffix;

    $userClickAdd = "update $table set click = '$userClick' where nid = '$userId'";
    $query = mysql_query($userClickAdd ) or die ('Update clicks error!');
}





//传递分类的id，然后根据分类id，查出其上级分类的名字。
//为了输出格式的灵活，所以第二个参数$termnumber用来判断返回term0~term5

//传递产品的id，查出term0至term5个个分类的名字和id
function getallterm($targetid, $getdata)
{

    global $table_Prefix;
    global $sys_list_table;
    global $sys_term_table;
//设置需要搜索的表格的前缀后缀
    $table_Suffix = $sys_term_table;
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀


    $result = mysql_query("SELECT * FROM $table where nid = '$targetid'"); //查出这个id的产品的各个termid

    while ($row = mysql_fetch_array($result)) {

        $target_interm = $row['interm'];
        $target_term1 = $row['term1'];
    }


    if ($target_interm != 'root') {

//	根据这些term的id查出各自的名字

        $result2 = mysql_query("SELECT * FROM $table where nid = '$target_term1'");

        while ($row = mysql_fetch_array($result2)) {
            $term0_name = $row['name'];
            $term0_nid = $row['nid'];
            $term0_url = $row['url'];
        }


    }

    //mysql_close($con);


    if ($getdata == 'id') {
        return $target_term1;
    } else if ($getdata == 'url') {
        return $term0_url;
    } else {
        return $term0_name;
    }


}





function id_get_term($targetid, $target_date)
{
    global $table_Prefix;
    global $sys_list_table;
    global $sys_term_table;
//设置需要搜索的表格的前缀后缀
    $table_Suffix = $sys_term_table;
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀


    $result = mysql_query("SELECT * FROM $table where nid = '$targetid'"); //查出这个id的产品

    while ($row = mysql_fetch_array($result)) {
        $target_nid = $row['nid'];
        $target_name = $row['name'];
        $target_url = $row['url'];
    }


    if ($target_date == "url") {
        $return_date = $target_url;
    } else {
        $return_date = $target_name;
    }

    return $return_date;


}






// 分析返回用户操作系统名称//
function getSystem()
{
    $sys = $_SERVER['HTTP_USER_AGENT'];
    if (stripos($sys, "NT 6.1")) {
        return true;
    } else if (stripos($sys, "NT 6.2")) {
        return true;
    } else if (stripos($sys, "NT 6.0")) {
        return true;
	} else if (stripos($sys, "NT 6.3")) {
        return true;
    } else if (stripos($sys, "NT 6.4")) {
        return true;
    } else if (stripos($sys, "NT 10.0")) {
        return true;
    }else if (stripos($sys, "NT 5.1")) {
        return true;
    } else if (stripos($sys, "NT 5.2")) {
        return true;
    } else if (stripos($sys, "NT 5")) {
        return true;
    } else if (stripos($sys, "NT 4.9")) {
        return true;
    } else if (stripos($sys, "NT 4")) {
        return true;
    } else if (stripos($sys, "98")) {
        return true;
    } else if (stripos($sys, "95")) {
        return true;
    } else {
        return false;
    }
}

/* 上面是判断操作系统的，并返回值，如果是电脑操作系统返回TRUE*/


?>
<?//
////判断字符长度大于设定值，则阶段并加省略号
////中文字3字节，英文数字1字节
//function cutstr($text, $long)
//{
//    if (utf8_strlen($text) > $long) {
//        $function_result = utf8Substr($text, 0, $long) . "...";
//    } else {
//        $function_result = $text;
//    }
//
//    return $function_result;
//}
//
//?>
<?//
////判断中文字符串的字数
//function utf8_strlen($str)
//{
//    $count = 0;
//    for ($i = 0; $i < strlen($str); $i++) {
//        $value = ord($str[$i]);
//        if ($value > 127) {
//            $count++;
//            if ($value >= 192 && $value <= 223) $i++;
//            elseif ($value >= 224 && $value <= 239) $i = $i + 2;
//            elseif ($value >= 240 && $value <= 247) $i = $i + 3;
//            else die('Not a UTF-8 compatible string');
//        }
//        $count++;
//    }
//    return $count / 2;
//}
//
//?>
<?

//读取某个板块的所有内容,存放在plate中.

function plate_detail($str)
{
global $table_Prefix;
global $sys_list_table;
global $sys_term_table;
global $sys_plate_table;
//设置需要搜索的表格的前缀后缀
$table_Suffix = $sys_plate_table;
$table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀

$result = mysql_query("SELECT * FROM $table where mid = '$str' ");

while ($row = mysql_fetch_array($result)) {
    $plate['nid']= $row['nid'];
    $plate_['mid'] = $row['mid'];
    $plate_['name'] = $row['name'];
    $plate_['banner_id'] = $row['banner_id'];
    $plate_['detail'] = $row['detail'];
    $plate_['introduce ']= $row['introduce'];
    $plate_['menu_primary_publish'] = $row['menu_primary_publish'];
    $plate_['menu_second_publish'] = $row['menu_second_publish'];
    $plate_['menu_third_publish'] = $row['menu_third_publish'];
    $plate_['title'] = $row['title'];
    $plate_['keywords ']= $row['keywords'];
    $plate_['description ']= $row['description'];
    $plate_['term_public'] = $row['term_public'];
    $plate_['list_public'] = $row['list_public'];
    $plate_['sys_belong'] = $row['sys_belong'];
    $plate_['page_list_num'] = $row['page_list_num'];
    $plate_['term_list_page'] = $row['term_list_page'];
    $plate_['detail_list_page'] = $row['detail_list_page'];
    $plate_['term'] = $row['term'];
    $plate_['url ']= $row['url'];
}

return $plate;

}





//
//获取plate的内容，需要整合成一个,后期直接读取整个板块的内容，就可以减小读取数据库次数
//获取plate的内容，需要整合成一个,后期直接读取整个板块的内容，就可以减小读取数据库次数
//获取plate的内容，需要整合成一个,后期直接读取整个板块的内容，就可以减小读取数据库次数
//获取plate的内容，需要整合成一个
//获取plate的内容，需要整合成一个





//获取指定板块的伪静态url================

function menu_url($str)
{
    global $table_Prefix;
    global $sys_list_table;
    global $sys_term_table;
    global $sys_plate_table;
//设置需要搜索的表格的前缀后缀
    $table_Suffix = $sys_plate_table;
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀

    $result = mysql_query("SELECT * FROM $table where mid = '$str' ");

    while ($row = mysql_fetch_array($result)) {
        $url = $row['url'];
    }

    return $url;

}






//获取指定板块的名称
function menu_name($str)
{
    global $table_Prefix;
    global $sys_plate_table;
//设置需要搜索的表格的前缀后缀
    $table_Suffix = $sys_plate_table;
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀

    $result = mysql_query("SELECT * FROM $table where mid = '$str' ");

    while ($row = mysql_fetch_array($result)) {
        $name = $row['name'];
    }

    return $name;

}

//获取指定板块的标题
function menu_title($str)
{
    global $table_Prefix;
    global $sys_plate_table;
    $table_Suffix = $sys_plate_table;
    $table = $table_Prefix . $table_Suffix;

    $result = mysql_query("SELECT * FROM $table where mid = '$str' ");

    while ($row = mysql_fetch_array($result)) {
        $title = $row['title'];
    }

    return $title;

}


//获取指定板块的名称
function menu_term($str)
{
    global $table_Prefix;
    global $sys_plate_table;
//设置需要搜索的表格的前缀后缀
    $table_Suffix = $sys_plate_table;
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀

    $result = mysql_query("SELECT * FROM $table where mid = '$str' ");

    while ($row = mysql_fetch_array($result)) {
        $term = $row['term'];
    }

    return $term;

}


//获取指定板块的列表的长度
function menu_page_list($str)
{
    global $table_Prefix;
    global $sys_plate_table;
//设置需要搜索的表格的前缀后缀
    $table_Suffix = $sys_plate_table;
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀

    $result = mysql_query("SELECT * FROM $table where mid = '$str' ");

    while ($row = mysql_fetch_array($result)) {
        $page_list = $row['page_list_num'];
    }

    return $page_list;

}


//获取指定板块的发布状态
function menu_term_publish($str)
{
    global $table_Prefix;
    global $sys_plate_table;
//设置需要搜索的表格的前缀后缀
    $table_Suffix = $sys_plate_table;
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀

    $result = mysql_query("SELECT * FROM $table where mid = '$str' ");
	
    while ($row = mysql_fetch_array($result)) {
        $term_publish = $row['term_public'];
    }

    return $term_publish;

}



//获取板块的人工编号
function sys_plate($strName,$strReturn){
    global $table_Prefix;
    global $sys_plate_table;
    $table_Suffix = $sys_plate_table;
    $table = $table_Prefix . $table_Suffix;

    $result = mysql_query("SELECT * FROM $table where mid = '$strName' ");

    while ($row = mysql_fetch_array($result)) {
        $strResult = $row[$strReturn];
    }

    return $strResult;
}





//获取浏览器名称
function getBrowser(){
    $agent=$_SERVER["HTTP_USER_AGENT"];
    if(strpos($agent,'MSIE')!==false || strpos($agent,'rv:11.0')) //ie11判断
    return "ie";
    else if(strpos($agent,'Firefox')!==false)
    return "firefox";
    else if(strpos($agent,'Chrome')!==false)
    return "chrome";
    else if(strpos($agent,'Opera')!==false)
    return 'opera';
    else if((strpos($agent,'Chrome')==false)&&strpos($agent,'Safari')!==false)
    return 'safari';
    else 
    return 'unknown';
}


//获取浏览器版本
function getBrowserVer(){
    if (empty($_SERVER['HTTP_USER_AGENT'])){    //当浏览器没有发送访问者的信息的时候
        return 'unknow';
    }
    $agent= $_SERVER['HTTP_USER_AGENT'];   
    if (preg_match('/MSIE\s(\d+)\..*/i', $agent, $regs))
        return $regs[1];
    elseif (preg_match('/FireFox\/(\d+)\..*/i', $agent, $regs))
        return $regs[1];
    elseif (preg_match('/Opera[\s|\/](\d+)\..*/i', $agent, $regs))
        return $regs[1];
    elseif (preg_match('/Chrome\/(\d+)\..*/i', $agent, $regs))
        return $regs[1];
    elseif ((strpos($agent,'Chrome')==false)&&preg_match('/Safari\/(\d+)\..*$/i', $agent, $regs))
        return $regs[1];
    else
        return 'unknow';
}

//print_r(getBrowser());
//print_r(getBrowserVer());
//echo '<br>'.$_SERVER["HTTP_USER_AGENT"];














//================================
// 获取当前路径包含前缀及路径及参数
//================================



function curPageURL() 
{
  $pageURL = 'http';

  if ($_SERVER["HTTPS"] == "on") 
  {
    $pageURL .= "s";
  }
  $pageURL .= "://";

  if ($_SERVER["SERVER_PORT"] != "80") 
  {
    $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  } 
  else 
  {
    $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  }
  return $pageURL;
}















//================================
// 记录日志:加入记录操作功能。

// ip           =地址=函数获取
// user         =角色=session/游客
// control      =操作=形参传递
// target_table =目标=形参传递
// target_data  =数据=形参传递
// date         =日期=自动录入
// remark       =备注=形参传递
//================================

function blackbox($action,$target_table,$target_data,$remark,$location){
	
//设置需要搜索的表格的前缀后缀
    global $table_Prefix;
    $table_Suffix = "log";
    $table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀







$_ip           =$_SERVER["REMOTE_ADDR"]; 
$user          =$_SESSION['username'];

if((!$user) || ($user==""))
{$_user="未登录用户";}
else{$_user=$user;}
 

$_action       =$action;
$_target_table =$target_table;
$_target_data  =$target_data;
$_remark       =$remark;
$_location     =$location;
 



$sql = "INSERT INTO $table (
ip,
user,
action,
target_table,
target_data,
remark,
location
)

value (
'$_ip',
'$_user',
'$_action',
'$_target_table',
'$_target_data',
'$_remark',
'$_location'
)";



$table_list=list_tables($dbName);

 
if(in_array($table,$table_list)){
$query = mysql_query($sql) or die (mysql_error());
    //echo "含黑盒表格";
}

{
    echo "不含黑盒表格";
}


return "BBF";//black box finished
}




?>